/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2013
 */


package com.smg.community.controller;

import java.util.HashMap;
import java.util.Map;

import java.text.SimpleDateFormat;
import java.util.Date;

import javacommon.base.BaseRestSpringController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.web.scope.Flash;

import java.util.*;

import javacommon.base.*;
import javacommon.util.*;

import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;

import com.smg.community.model.*;
import com.smg.community.dao.*;
import com.smg.community.service.*;
import com.smg.community.vo.query.*;

/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */

@Controller
@RequestMapping("/smgmemberfocus")
public class SmgMemberFocusController extends BaseRestSpringController<SmgMemberFocus,java.lang.Integer>{
	
	@RequestMapping(value="/focus/{memberId}")
	@Transactional
	public @ResponseBody Map<String,String> focus(@PathVariable int memberId,HttpServletRequest request,ModelMap model){
		SmgMember loginMember = getLoginMember(request);
		Map<String,String> map = new HashMap<String,String>();
		if ( loginMember == null){
			map.put("state", "-1");
			map.put("desc","您未登录或者登录超时");
			return null;
		}
		try {
			SmgMemberFocus memberFocus = new SmgMemberFocus();
			memberFocus.setFromMemberId(loginMember.getMemberId());
			memberFocus.setFocusMemberId(memberId);
			smgMemberFocusManager.save(memberFocus);
			//判断是否已经相互关注 添加好友
			SmgMemberFocus memberFocus2 = smgMemberFocusManager.getByMemberIdAndFocuseId(memberId, loginMember.getMemberId());
			if ( memberFocus2 != null){
				SmgMemberFriend friend = new SmgMemberFriend();
				friend.setMemberIdA(memberId);
				friend.setMemberIdB(loginMember.getMemberId());
				smgMemberFriendManager.save(friend);
			}
			smgMemberManager.addFocusAndFansCount(loginMember.getMemberId(),memberId);
			saveMemberInSession(request, smgMemberManager.getById( loginMember.getMemberId()));
		}catch(Exception e){
			e.printStackTrace();
		}
		map.put("state","1");
		return map;
	}
	
	
	
	@RequestMapping(value="/deleteFocus/{memberId}")
	@Transactional
	public @ResponseBody Map<String,String> deleteFocus(@PathVariable int memberId,HttpServletRequest request,ModelMap model){
		SmgMember loginMember = getLoginMember(request);
		Map<String,String> map = new HashMap<String,String>();
		if ( loginMember == null){
			map.put("state", "-1");
			map.put("desc","您未登录或者登录超时");
			return null;
		}
		//取消关注 则同时删除2个人的好友关系
		smgMemberFocusManager.deleteByFromMemberIdAndFocusMemberId(loginMember.getMemberId(),memberId);
		smgMemberManager.subFocusAndFansCount(loginMember.getMemberId(),memberId);
		saveMemberInSession(request, smgMemberManager.getById( loginMember.getMemberId()));
		map.put("state","1");
		return map;
	}
	
	@RequestMapping(value="/listFans/{memberId}-{pageNumber}")
	public String listFans(@PathVariable int memberId,@PathVariable int pageNumber,HttpServletRequest request,ModelMap model){
		setCommunityCommonModel(request, model);
		setHeadTabIndex(model, 6);
		SmgMember member = smgMemberManager.getById(memberId);
		if ( member == null){
			return toErrorPage(model, "你所查看的用户不存在!");
		}
		SmgMemberQuery query = new SmgMemberQuery();
		setStartAndLimit(query, pageNumber);
		query.setFansMemberId(memberId);
		Page page  =  smgMemberManager.findPage(query);
		model.addAttribute("page",page);
		model.addAttribute("pageNumber",pageNumber);
		model.addAttribute("viewMember",member);
		SmgMember loginMember = getLoginMember(request);
		model.addAttribute("isSelf",false);
		if ( loginMember != null ){
			if ( loginMember.getMemberId().intValue() == member.getMemberId().intValue()){
				smgMemberFocusManager.updateIsnew(member.getMemberId().intValue());
				model.addAttribute("isSelf",true);
			}else{
				model.addAttribute("isSelf",false);
			}
		}
		model.addAttribute("type","11");
		return "member/fans";
	}
	
	@RequestMapping(value="/listFriend/{memberId}-{pageNumber}")
	public String listFriend(@PathVariable int memberId,@PathVariable int pageNumber,HttpServletRequest request,ModelMap model){
		setCommunityCommonModel(request, model);
		setHeadTabIndex(model, 6);
		SmgMember member = smgMemberManager.getById(memberId);
		if ( member == null){
			return toErrorPage(model, "你所查看的用户不存在!");
		}
		SmgMemberQuery query = new SmgMemberQuery();
		setStartAndLimit(query, pageNumber);
		query.setFriendMemberId(memberId);
		smgMemberManager.findPage(query);
		model.addAttribute("pageNumber",pageNumber);
		model.addAttribute("viewMember",member);
		Page page  =  smgMemberManager.findPage(query);
		model.addAttribute("page",page);
		model.addAttribute("type","11");
		SmgMember loginMember = getLoginMember(request);
		model.addAttribute("isSelf",false);
		if ( loginMember != null ){
			if ( loginMember.getMemberId().intValue() == member.getMemberId().intValue()){
				model.addAttribute("isSelf",true);
			}else{
				model.addAttribute("isSelf",false);
			}
		}
		return "member/friend";
	}
	
	@RequestMapping(value="/listFocus/{memberId}-{pageNumber}")
	public String listFocus(@PathVariable int memberId,@PathVariable int pageNumber,HttpServletRequest request,ModelMap model){
		setCommunityCommonModel(request, model);
		setHeadTabIndex(model, 6);
		SmgMember member = smgMemberManager.getById(memberId);
		if ( member == null){
			return toErrorPage(model, "你所查看的用户不存在!");
		}
		SmgMemberQuery query = new SmgMemberQuery();
		setStartAndLimit(query, pageNumber);
		query.setFocusMemberId(memberId);
		smgMemberManager.findPage(query);
		model.addAttribute("pageNumber",pageNumber);
		model.addAttribute("viewMember",member);
		Page page  =  smgMemberManager.findPage(query);
		model.addAttribute("page",page);
		model.addAttribute("type","11");
		SmgMember loginMember = getLoginMember(request);
		model.addAttribute("isSelf",false);
		if ( loginMember != null ){
			if ( loginMember.getMemberId().intValue() == member.getMemberId().intValue()){
				model.addAttribute("isSelf",true);
			}else{
				model.addAttribute("isSelf",false);
			}
		}
		return "member/focus";
	}
}

